'''
Created on Nov 3, 2016

@author: Zhaokun Fu
'''

import libvirt

class QemuConnection(object):
    '''
    classdocs
    '''
    

    def __init__(self):
        '''
        Constructor
        '''
        self.connection = None
        
    
    def get(self):
        if not self.connection:
            self.connection = libvirt.open("qemu:///system")
        return self.connection

    
    def close(self):
        if self.connection:
            self.connection.close()
            
            
    def release(self):
        if self.connection:
            self.connection.close()
            del self.connection
        
        if self.lock:
            del self.lock
            
    
qemu_connection = None

def get_connection():
    global qemu_connection
    if not qemu_connection:
        qemu_connection = QemuConnection().get()
    return qemu_connection


def close_connection():
    global qemu_connection
    if qemu_connection:
        qemu_connection.close()


def release_connection():
    global qemu_connection
    if qemu_connection:
        qemu_connection.release()        

    